package com.arithmeticEveryDay.blueBridge;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;

/*
青蛙跳杯子
 */
public class Main102 {

    private String origin;
    private String target;

    public Main102(String origin, String target) {
        this.origin = origin;
        this.target = target;
    }

    /*
                                   1                                            2
                       /          /        \                 /            /        \           \
                      0         2           3               0           1           3           4
                /   /  \
               1   2    3
    ...


     */
    public int bfs() {
        LinkedList<Node> queue = new LinkedList<Node>();
        int spaceIndex = origin.indexOf('*');
        queue.add(new Node(spaceIndex, origin, 0));
        HashSet<String> visited = new HashSet<String>();
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            if (node.status.equals(target)) {
                return node.step;
            }
            int curSpaceIndex = node.spaceIndex;
            origin = node.status;
            for (int i = -3; i <= 3; i++) {
                StringBuffer SBOrigin = new StringBuffer(origin);
                int toLeaveIndex = curSpaceIndex + i;
                if (toLeaveIndex >= 0 && toLeaveIndex < origin.length() && toLeaveIndex != curSpaceIndex) {
                    SBOrigin.setCharAt(curSpaceIndex, origin.charAt(toLeaveIndex));
                    SBOrigin.setCharAt(toLeaveIndex, '*');
                    origin = SBOrigin.toString();
                    if (!visited.contains(origin)) {
                        visited.add(origin);
                        queue.add(new Node(toLeaveIndex, origin, node.step+1));
                    }
                    origin = node.status;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String origin = scan.next();
        String target = scan.next();

        Main102 m = new Main102(origin, target);
        System.out.println(m.bfs());
    }

}
class Node {
    int spaceIndex;
    String status;
    int step;

    public Node(int spaceIndex, String status, int step) {
        this.spaceIndex = spaceIndex;
        this.status = status;
        this.step = step;
    }

}